package cn.tedu.charging.device.service.impl;

import cn.tedu.charging.common.pojo.param.GunStatusUpdateParam;
import cn.tedu.charging.common.pojo.vo.StationInfoVO;
import cn.tedu.charging.device.dao.repository.StationCacheFactory;
import cn.tedu.charging.device.dao.repository.StationRepository;
import cn.tedu.charging.device.pojo.param.StationNearQuery;
import cn.tedu.charging.device.pojo.po.StationCanalPO;
import cn.tedu.charging.device.pojo.po.StationPO;
import cn.tedu.charging.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private StationCacheFactory stationCacheFactory;
    @Autowired
    private StationRepository stationRepository;

    @Override
    public StationInfoVO getStationInfo(Integer id) {
        StationInfoVO stationInfoVO = new StationInfoVO();
        stationInfoVO.setId(id);
        return stationInfoVO;
    }

    @Override
    public Boolean updateGunStatus(GunStatusUpdateParam param) {
        return true;
    }

    @Override
    public List<StationInfoVO> getNearStation(StationNearQuery stationNearQuery) {
        List<StationPO> stationPOs = stationCacheFactory.getStationCacheRepository().search(stationNearQuery.getLng(), stationNearQuery.getLat(), stationNearQuery.getRadius());
        List<StationInfoVO> result = new ArrayList<>();
        for(StationPO stationPO : stationPOs){
            StationInfoVO stationInfoVO = new StationInfoVO();
            BeanUtils.copyProperties(stationPO,stationInfoVO);
            result.add(stationInfoVO);
        }
        return result;
    }

    @Override
    public void save(List<StationPO> stationPOS) {
        stationCacheFactory.getStationCacheRepository().saveAllStation(stationPOS);
    }

    @Override
    public List<StationPO> getAllStation() {
        return stationRepository.getAllStation();
    }

    @Override
    public void insert(StationCanalPO stationCanalPO) {
        stationCacheFactory.getStationCacheRepository().insert(stationCanalPO);
    }

    @Override
    public void update(StationCanalPO after) {
        stationCacheFactory.getStationCacheRepository().update(after);
    }

    @Override
    public void delete(StationCanalPO stationCanalPO) {
        stationCacheFactory.getStationCacheRepository().delete(stationCanalPO);
    }
}
